package com.qf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCoupon;
import com.qf.service.CouponService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/30 16:21
 */
@CrossOrigin("*")
@RequestMapping("/coupon")
@RestController
public class CouponController {

    @Autowired
    private CouponService couponService;

    @RequestMapping("/list")
    public  Object queryByPage(String name,Integer page,Integer limit,String sort,String order){

        //创建分页对象
        IPage<DtsCoupon> iPage = couponService.queryByPage(name, page, limit, sort, order);

        //查询总条数
        long total = iPage.getTotal();

        //查询每页页数
        List<DtsCoupon> list = iPage.getRecords();

        Map<String, Object> data = new HashMap<>();

        data.put("total", total);
        data.put("items", list);
        return ResponseUtil.ok(data);

    }

    @RequestMapping("/create")
    public Object add(@RequestBody DtsCoupon coupon){
        DtsCoupon addCoupon = couponService.add(coupon);
        return ResponseUtil.ok(addCoupon);
    }

}
